package org.centsuse.intention_spring.invention.controller;

import lombok.extern.slf4j.Slf4j;
import org.centsuse.intention_spring.invention.entities.http.ChatRequest;
import org.centsuse.intention_spring.invention.entities.http.ChatResponse;
import org.centsuse.intention_spring.invention.service.Chat2AIService;
import org.centsuse.intention_spring.invention.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

// REST 控制器
@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/api")
@Slf4j
public class ChatbotController {

    @Autowired
    private Chat2AIService chat2AIService;

    @PostMapping("/multi_question")
    public ResponseEntity<?> handleMultiQuestion(@RequestBody ChatRequest request) {
        log.info("[ChatbotController][handleMultiQuestion]: request {}", request);
        if (request == null || StringUtils.isEmpty(request.getInput())) {
            return ResponseEntity.badRequest().body(Map.of("error", "No question provided"));
        }

        try {
            ChatResponse response = chat2AIService.processMultiQuestion(request);
            log.info("[ChatbotController][handleMultiQuestion]: response {}", response);
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            return ResponseEntity.internalServerError().body(Map.of("error", e.getMessage()));
        }
    }
}
